package cn.zyy.program;

import cn.zyy.controller.ScoreController;
import cn.zyy.controller.StudentController;
import cn.zyy.entity.Student;

import java.util.*;

public class StudentTest {
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        mainSystem();
        System.out.println("谢谢使用！");
    }
    //主程序
    public static void mainSystem() {
        do {
            System.out.println("******欢迎使用学生成绩管理系统******");
            System.out.println("1、学生管理   2、成绩管理  3、退出系统");
            System.out.println("请选择：");
            int choose = input.nextInt();
            while (choose > 4 || choose < 0) {
                System.out.println("选择错误，请重新选择：");
            }
            switch (choose){
                case 1:
                    studentSystem();
                    continue;
                case 2:
                    scoreSystem();
                    continue;
                default:
                    break;
            }
            break;
        }while(true);
    }
    //学生管理
    public static void studentSystem() {
        do {
            System.out.println("******学生管理******");
            System.out.println("1、增加学生  2、删除学生  3、分班  4、查询所有学生");
            System.out.println("请选择(输入其他返回上一级)：");
            int choose = input.nextInt();
            switch (choose){
                case 1: //增加学生
                    addStudent();
                    continue;
                case 2://根据id删除学生
                    deleteStudent();
                    continue;
                case 3://根据学生id和班级名称给学生分班（根据班级名称查询班级ID，然后根据学生id，更新学生的班级信息）
                    classification();
                    continue;
                case 4://查询所有学生信息,要求显示对应班级名称
                    selectStudent();
                    continue;
                default:
                    break;
            }
            break;
        }while(true);
    }
    //成绩管理
    public static void scoreSystem() {
        do {
            System.out.println("******成绩管理******");
            System.out.println("1、记录成绩  2、查询某个科目所有学生成绩");
            System.out.println("请选择(输入其他返回上一级)：");
            int choose = input.nextInt();
            switch (choose){
                case 1: //根据学生姓名和科目名称给学生记录成绩
                    addScore();
                    continue;
                case 2://根据科目名称查询所有学生的成绩，并降序排列
                    selectScore();
                    continue;
                default:
                    break;
            }
            break;
        }while(true);
    }
    //增加学生
    public static void addStudent() {
        StudentController sco = new StudentController();
        System.out.println("请输入学生ID：");
        int stuId = input.nextInt();
        System.out.println("请输入学生姓名：");
        String stuName = input.next();
        System.out.println("请输入班级ID：");
        int classId = input.nextInt();
        Student student = Student.builder().stuId(stuId).stuName(stuName).classId(classId).build();
        Boolean isAdd = sco.addStudent(student);
        if (isAdd) {
            System.out.println("添加成功！");
        }else {
            System.out.println("添加失败！");
        }
    }
    //根据id删除学生
    public static void deleteStudent() {
        StudentController sco = new StudentController();
        System.out.println("请输入要删除学生的ID：");
        int stuId = input.nextInt();
        Boolean isDel = sco.deleteStudent(stuId);
        if (isDel) {
            System.out.println("删除成功！");
        }else {
            System.out.println("删除失败！");
        }
    }
    //根据学生id和班级名称给学生分班（根据班级名称查询班级ID，然后根据学生id，更新学生的班级信息）
    public static void classification() {
        StudentController sco = new StudentController();
        System.out.println("请输入学生的ID：");
        int stuId = input.nextInt();
        System.out.println("请输入班级名称：");
        String className = input.next();
        Integer classId = sco.classification(className);
        Boolean isClass = sco.classification(stuId, classId);
        if (isClass) {
            System.out.println("分班成功！");
        }else {
            System.out.println("分班失败！");
        }
    }
    //查询所有学生信息,要求显示对应班级名称
    public static void selectStudent() {
        StudentController sco = new StudentController();
        HashMap<String, String> student = sco.selectStudent();
        Set<String> stuNames = student.keySet();
        Iterator<String> it = stuNames.iterator();
        String stuName = "";
        String className = "";
        while (it.hasNext()) {
            stuName = it.next();
            className = student.get(stuName);
            System.out.println("姓名："+stuName+"\t班级："+className);
        }
    }
    //根据学生姓名和科目名称给学生记录成绩
    public static void addScore() {
        ScoreController sco = new ScoreController();
        System.out.println("请输入学生姓名：");
        String stuName = input.next();
        System.out.println("请输入科目名称：");
        String courseName = input.next();
        System.out.println("请输入分数：");
        double score = input.nextDouble();
        Map<String, Integer> map = sco.addScore(stuName, courseName);
        int stuId = map.get("stuId");
        int courseId = map.get("courseId");
        boolean isAddScore = sco.isAddScore(stuId, courseId);
        Boolean isAdd = sco.addScore(stuId, courseId, score,isAddScore);
        if (isAdd) {
            System.out.println("添加成功！");
        }else {
            System.out.println("添加失败！");
        }
    }
    //根据科目名称查询所有学生的成绩，并降序排列
    public static void selectScore() {
        ScoreController sco = new ScoreController();
        System.out.println("请输入要查询的科目名称：");
        String courseName = input.next();
        int courseId = sco.selectScore(courseName);
        ArrayList<Student> studentScores = sco.selectScore(courseId);
        System.out.println(courseName+"的所有学生成绩如下：");
        for (int i = 0; i < studentScores.size(); i++) {
            System.out.println(studentScores.get(i));
        }
    }

}
